//对于axios的二次封装
//引入axios
import axios from 'axios';
import Router from '@/route'
import {Message} from "element-ui";
//创建axios实例
const requests = axios.create({
    baseURL: "http://localhost:9999",
    timeout: 5000
});
//请求拦截器在请求发出去之前处理数据
requests.interceptors.request.use((config) => {
    //将员工id、token 放在请求头里
    config.headers['id'] = localStorage.getItem('staffId')
    config.headers['token'] = localStorage.getItem('token')
    console.log(localStorage.getItem('staffId'))
    console.log(localStorage.getItem('token'))
    return config;
})
//响应拦截器
requests.interceptors.response.use((values) => {
    if (values.data.code === 401 || values.data.code === 402) {
        Message.error(values.data.message)
        Router.push({path: "/login"});
        return values.data;
    } else {
        //成功的回调
        return values.data;
    }
}, (error) => {
    //失败的回调
    return Promise.reject(new Error(error));
})

export default requests;

